package sda.app_01.fragment;

import android.content.ContentResolver;
import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;

import net.tsz.afinal.FinalActivity;
import net.tsz.afinal.annotation.view.ViewInject;

import java.io.File;
import java.io.IOException;

import sda.app_01.R;

/**
 * Created by scorpio on 15-3-18.
 */
public class fragment_1 extends Fragment implements View.OnClickListener {

    @ViewInject(id = R.id.btn_camera, click = "onClick")
    Button btn_camera;
    @ViewInject(id = R.id.btn_photo, click = "onClick")
    Button btn_photo;
    @ViewInject(id = R.id.imageview)
    ImageView imageView;

    String TAG = "fragment_1";

    String fileName = "";
    File tempFile;

    private static final int CAMERA_TAKE = 1;
    private static final int CAMERA_SELECT = 2;

    String PATH = Environment.getExternalStorageDirectory() + "/DCIM";
    String name;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_1, null);
        FinalActivity.initInjectedView(this, view);
        initFile();
        return view;
    }

    public void initFile() {
        if (fileName.equals("")) {
            String path = Environment.getExternalStorageDirectory() + File.separator + "sdaduanbilei" + File.separator;
            Log.d(TAG, "path:" + path);
            fileName = path + "user_head_photo.jpg";
            tempFile = new File(fileName);
        }
    }


    @Override
    public void onClick(View v) {
        int id = v.getId();
        switch (id) {
            case R.id.btn_camera:
                Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);//action is capture
                intent.putExtra("return-data", true);
                startActivityForResult(intent, CAMERA_TAKE);
                break;
            case R.id.btn_photo:
                Intent i = new Intent(Intent.ACTION_GET_CONTENT, null);
                i.setType("image/*");
                startActivityForResult(i, CAMERA_SELECT);
                break;
        }
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        ContentResolver contentResolver = getActivity().getContentResolver();
        switch (requestCode) {
            case CAMERA_TAKE:
                Bundle bundle = data.getExtras();
                Bitmap bitmap = (Bitmap) bundle.get("data");
                imageView.setImageBitmap(bitmap);
                break;
            case CAMERA_SELECT:
                Uri uri1 = data.getData();
                try {
                    Bitmap bitmap1 = MediaStore.Images.Media.getBitmap(contentResolver, uri1);

                   // bitmap1.compress(Bitmap.CompressFormat.JPEG,30,new ByteArrayOutputStream()); // 图片压缩 表示压缩 70%
//                    http://www.oschina.net/code/snippet_216465_36833
                    imageView.setImageBitmap(bitmap1);
                } catch (IOException e) {
                    e.printStackTrace();
                }
                break;
        }
    }
}
